class Bus {
  constructor() {
    // 收集订阅信息,调度中心
    this.list = {};
  }
  // 订阅
  $on(name, fn) {
    this.list[name] = this.list[name] || [];
    this.list[name].push(fn);
  }
  // 发布
  $emit(name, ...data) {
    if (this.list[name]) {
      this.list[name].forEach((fn) => fn(...data));
    }
  }
  // 取消订阅
  $off(name) {
    if (this.list[name]) {
      delete this.list[name];
    }
  }
  // 批量订阅
  $mapOn(arr = [], fn) {
    arr.forEach((name) => this.$on(name, fn));
  }
  // 批量发布
  $mapEmit(arr = [], ...data) {
    arr.forEach((name) => this.$emit(name, ...data));
  }
  // 批量取消订阅
  $mapOff(arr = []) {
    arr.forEach((name) => this.$off(name));
  }
  // 清除所有事件
  $offAll() {
    this.$mapOff(Object.keys(this.list));
  }
}
const bus = new Bus();
export default bus;
